// gulp
var gulp = require('gulp');
var distro = '../resources/public/';
// plugins
var connect = require('gulp-connect');
var concat = require('gulp-concat');
var del = require('del');
var less = require('gulp-less');
var path = require('path');

var watcherJS = null ;
var watcherLESS = null ;
var watcherHTML = null;



gulp.task('devWorkflow',function(){
    distro = 'dist/';

    var watcherJS = gulp.watch('app/js/**/*.js', ['scripts']);
    var watcherLESS = gulp.watch('app/less/**/*.less', ['less']);
    var watcherHTML = gulp.watch(['app/*.html','app/partials/**/*.html'], ['copy-html-files']);

    watcherJS.on('change', function(event) {
        console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
    });

    watcherLESS.on('change', function(event) {
        console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
    });

    watcherHTML.on('change', function(event) {
        console.log('File ' + event.path + ' was ' + event.type + ', running tasks...');
    });

});

gulp.task('clean', function(){
    del.sync(distro+"**",{force:true});
});


gulp.task('copy-libs', function () {
  gulp.src(['app/lib/**/','!app/lib/bootstrap{,/**}'])
    .pipe(gulp.dest(distro+'lib/'));
  
});

gulp.task('scripts', function() {
  gulp.src(['app/js/**/*.js'])
    .pipe(concat('app.js'))
    .pipe(gulp.dest(distro))
});

gulp.task('less', function() {
  gulp.src(['app/less/master.less'])
   .pipe(less({
      paths: [ path.join(__dirname, 'less', 'includes') ]
    }))
    .pipe(gulp.dest(distro));
});

gulp.task('copy-html-files', function () {
  gulp.src('app/*.html')
    .pipe(gulp.dest(distro));
  gulp.src('app/partials/**/*.html')
    .pipe(gulp.dest(distro+'partials/'));
});

gulp.task('copy-img-files', function () {
  gulp.src('app/images/*.*')
    .pipe(gulp.dest(distro+'images/'));
  });

gulp.task('connectDev', function () {
  connect.server({
    root: distro,
    port: 9999
  });
});



gulp.task('build',
  ['clean','scripts','less','copy-img-files', 'copy-html-files','copy-libs']
);

gulp.task('dev',
  ['devWorkflow','scripts','less','copy-img-files', 'copy-html-files','copy-libs','connectDev']
);
